<?php
// +----------------------------------------------------------------------
// | Qbt CMS Plugin
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.qbt8.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 千佰特 <http://www.qbt8.com>
// +----------------------------------------------------------------------
namespace plugins\qbt_region\controller;

use cmf\controller\PluginAdminBaseController;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{
    /**
     * 地区管理
     * @adminMenu(
     *     'name'   => '地区管理',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '千佰特地区管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $result1 = Db::name('region')->where('level', 1)->order('sort desc')->select();
        foreach ($result1 as $k => $v){
            $info = Db::name('region')->where('pid', $v['id'])->find();
            if ($info){
                $v['isend'] = 1;
            }else{
                $v['isend'] = 0;
            }
            $result1[$k] = $v;
        }
        $this->assign("list", $result1);
        return $this->fetch('/admin_index');
    }

    public function ajaxget(){
        $id = $this->request->param('id');
        $list = Db::name('region')->where('pid', $id)->select();
        foreach ($list as $k => $v){
            $info = Db::name('region')->where('pid', $v['id'])->find();
            if ($info){
                $v['isend'] = 1;
            }else{
                $v['isend'] = 0;
            }
            $list[$k] = $v;
        }
        return json_encode($list);
    }

    //修改
    public function editpost()
    {
        $id = $this->request->param('id');
        $data['code'] = $this->request->param('code');
        $data['name'] = $this->request->param('name');
        $info = Db::name('region')->where("id = $id and code = '{$data['code']}' and name = '{$data['name']}'")->find();
        if ($info){
            $this->success('保存成功', cmf_plugin_url('QbtRegion://AdminIndex/index'));die;
        }
        $id = Db::name('region')->where('id', $id)->update($data);
        if ($id) {
            $this->success('修改成功', cmf_plugin_url('QbtRegion://AdminIndex/index'));
        } else {
            $this->error('修改失败！请稍后重试');
        }
    }

    //添加
    public function addpost()
    {
        $data['code'] = $this->request->param('code');
        $data['pid'] = $this->request->param('pid');
        $data['level'] = $this->request->param('level');
        $data['name'] = $this->request->param('name');
        $info = Db::name('region')->where(['name'=>$data['name']])->find();
        if ($info){
            $this->error('名称重复，添加失败！');die;
        }
        $id = Db::name('region')->insert($data);
        if ($id) {
            $this->success('添加成功', cmf_plugin_url('QbtRegion://AdminIndex/index'));
        } else {
            $this->error('添加失败！请稍后重试');
        }
    }

    public function edit()
    {
        $id = $this->request->param('id', 0, 'intval');
        $info = Db::name('region')->where(['id'=>$id])->find();
        $this->assign("id", $id);
        $this->assign("info", $info);
        return $this->fetch('/edit');
    }

    public function add()
    {
        $id = $this->request->param('id', 0, 'intval');
        $level = $this->request->param('level', 0, 'intval');
        $this->assign("pid", $id);
        $this->assign("level", $level+1);
        return $this->fetch('/add');
    }

    //删除
    public function del()
    {
        $id = $this->request->param('id', 0, 'intval');
        $info = Db::name('region')->where(['pid'=>$id])->find();
        if ($info){
            $this->success('请先删除子分类');
        }else{
            Db::name('region')->where(['id'=>$id])->delete();
            $this->success('删除成功');
        }
    }
}
